/**
 *author:yhb
 */
package com.towker.frame.controller.rbac;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.model.rbac.Subsystem;
import com.towker.frame.service.rbac.sys.SubsystemService;
import com.towker.frame.share.sys.user.UserShare;
import com.towker.web.MyRequest;

@Controller
@RequestMapping("/manage/rbac/sys")
public class SubsystemController
{
	//子系统业务处理对象。	private SubsystemService service;
	public void setSubsystemService(SubsystemService service){this.service = service;}
	/**
	 * 获得系统列表(分页显示)*/
	@SuppressWarnings("unchecked")
	@RequestMapping("/getSubsystem")
	public ModelAndView getSubsystem(HttpServletRequest request,@RequestParam(value = "page", defaultValue = "1")
	int page) throws IOException
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		PageModel<Subsystem> pageModel = service.getPage(page, pageSize, map);
		PageNav pageNav = new PageNav(request, pageModel);
		ModelAndView result = new ModelAndView();
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/rbac/sys/getSubsystem.jsp");
		return result;
	}
	/**
	 * 添加子系统
	 * @throws Exception*/
	@RequestMapping("/addSubsystem{viewName}")
	public ModelAndView addSubsystem(HttpServletResponse response, Subsystem subsystem, @PathVariable
	String viewName) throws Exception
	{
		ModelAndView result = null;
		if ("1".equals(viewName)){
			result = new ModelAndView();
			result.setViewName("/manage/rbac/sys/addSubsystem.jsp");
		}else if ("2".equals(viewName)){
			PrintWriter writer = response.getWriter();
			try{
				service.add(subsystem);
				writer.write("添加系统成功！");
			}catch (Exception e){e.printStackTrace();writer.write("添加系统失败！"+e.getCause());}
		}
		return result;
	}
	/**
	 * 删除子系统*/
	@RequestMapping("/delSubsystem")
	public void delSubsystem(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0L)));
		response.sendRedirect(req.getRefererURL());
	}
	/**
	 * 修改子系统*/
	@RequestMapping("/updSubsystem{viewName}")
	public ModelAndView updSubsystem(HttpServletRequest request, HttpServletResponse response,
	Subsystem subsystem, @PathVariable
	String viewName) throws IOException, Exception
	{
		ModelAndView result = null;
		if ("1".equals(viewName)){
			MyRequest req = new MyRequest(request);
			Long id=new Long(req.getLong("keyIndex"));
			Subsystem oldSubsystem=service.getById(id);
			result = new ModelAndView();
			result.addObject("subsystem", oldSubsystem);
			result.setViewName("/manage/rbac/sys/updSubsystem.jsp");
		}else if ("2".equals(viewName)){
			PrintWriter writer = response.getWriter();
			try{
				service.update(subsystem);
				writer.write("修改子系统信息成功！");
			}catch (Exception e){e.printStackTrace();writer.write("修改子系统信息失败！");}
		}
		return result;
	}
	/**
	 * 切换子系统*/
	@RequestMapping(value = "/getSysByUserid")
	public ModelAndView getSysByUserid(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		ModelAndView result = new ModelAndView();
		System.out.println(UserShare.getCurrentUserId(request));
		List<Subsystem> list = service.getSysByUserid(UserShare.getCurrentUserId(request));
		if(null==list||0==list.size()){
			result.setViewName("/commons/noSubsystem.jsp");
			return result;
		}else if(1==list.size()){
			UserShare.setCurrentSystemId(request, response, list.get(0).getId()+"");
			response.sendRedirect("/ryan/manage/main.htm?sysid=1");
			return null;
		}else if(1<list.size()){
			String sysid = String.valueOf(UserShare.getCurrentSystemId(request));
			result.addObject("sysid", sysid);
			result.addObject("sysList", list);
			result.setViewName("/manage/rbac/sys/getSysByUserid.jsp");
			return result;
		}
		return result;
	}
	/**
	 * 查看子系统*/
	@RequestMapping("/getSubsystemById")
	public ModelAndView getSubsystemById(HttpServletRequest request)
	{
		MyRequest req = new MyRequest(request);
		Subsystem subsystem = service.getById(req.getLong("keyIndex"));
		ModelAndView result = new ModelAndView();
		result.addObject("subsystem", subsystem);
		result.setViewName("/manage/rbac/sys/getSubsystemById.jsp");
		return result;
	}
}
